<template>
  <!--  jiera-->
  <div class="bg-white personal_info_update">
    <div class="handle_type">{{ handleType }}</div>
    <div class="form-conatiner">
      <ElForm :model="personalInfo" label-width="auto" style="max-width: 600px" :rules="phoneRules" ref="personalInfoForm">
        <ElFormItem label="昵称">
          <ElInput v-model="personalInfo.name" />
        </ElFormItem>
        <ElFormItem label="头像">
          <ElAvatar :src="personalInfo.avatar"  />
        </ElFormItem>
        <ElFormItem label="性别">
          <ElRadioGroup v-model="personalInfo.sex">
            <ElRadio v-for="item in sexType" :key="item.value" :value="item.value">{{
              item.label
            }}</ElRadio>
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem label="简介">
          <ElInput v-model="personalInfo.introduction" type="textarea" />
        </ElFormItem>
        <ElFormItem label="手机" prop="phoneNumber">
          <ElInput v-model="personalInfo.phoneNumber" type="phone"/>
        </ElFormItem>
        <ElFormItem label="地址">
          <ElInput v-model="personalInfo.address"  />
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" @click="handleSubmit" class="submit_btn">提交</ElButton>
        </ElFormItem>
      </ElForm>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, FormRules } from 'vue';
import { ElMessage, ElForm, ElFormItem, ElInput } from 'element-plus';
const personalInfoForm = ref(null);

import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import type { UploadFile } from 'element-plus'
import {useRoute, useRouter} from 'vue-router'
import { useUserStore } from '@/stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const handleType = ref('修改信息')

const currentLoginUserId = computed(()=>userStore.userInfo.uid||'1000000')

const personalInfo = reactive({
  id:currentLoginUserId,
  name: '',
  avatar: '',
  sex: '',
  introduction: '',
  phoneNumber: '',
  address: '',
})

const sexType = [
  {
    label: '男',
    value: 0,
  },
  {
    label: '女',
    value: 1,
  },
]

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)

const handleUpload = async (options: UploadEntity) => {
  await service.common.upload(options)
}
const handleRemove = (file: UploadFile) => {
  console.log(file)
}

const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}

const handleDownload = (file: UploadFile) => {
  console.log(file)
}

// 提交时form加上校验规则
const handleSubmit = async () => {
  // 先触发表单整体校验
  if (personalInfoForm.value) {
    const isValid = await personalInfoForm.value.validate();
    if (isValid) {
      // 所有字段校验通过，执行更新用户信息和页面跳转操作
      await service.user.updateUserInfo(personalInfo);
      // 更新userStore
      userStore.userInfo = {...userStore.userInfo,...personalInfo, uid: personalInfo.id };
      router.push('/main/personalHomepage?id=' + currentLoginUserId.value);
    }
  }
};

const getUserData=async(id:string|number)=>{
  // id
  await service.user.getUserInfo(id).then((res)=>{
    personalInfo.name=res.data.name;
    personalInfo.avatar=res.data.avatar;
    personalInfo.sex=res.data.sex;
    personalInfo.introduction=res.data.introduction;
    personalInfo.phoneNumber=res.data.phoneNumber;
    personalInfo.address=res.data.address;
  })
}
// 定义一个手机号校验指令
const phoneRules: FormRules<typeof personalInfo> = reactive({
  phoneNumber: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('手机号不能为空'));
        } else if (!/^1[3-9]\d{9}$/.test(value)) {
          callback(new Error('请输入正确的11位手机号码'));
        } else {
          callback();
        }
      },
      trigger: ['blur', 'submit']
    }
  ]
});


onMounted(()=>{
  getUserData(personalInfo.id)
})
</script>

<style lang="scss" scoped>
.personal_info_update {
  padding: 20px 0;

  .handle_type {
    padding: 0 20px 10px;
    margin-bottom: 10px;
    border-bottom: 2px solid rgba(0, 0, 0, 10%);
  }

  .form-conatiner {
    width: 65%;
    margin: 0 auto;

    .submit_btn {
      width: 300px;
      margin: 0 auto;
    }
  }
}
</style>
